package com.whf.android.jar.util.autonavi;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.Map;

/**
 * 高德导航工具类
 *
 * @author whf
 * @date 2021/11/18
 */
public final class AutoNaviUtils {


    /**
     * 跳转到高德导航
     *
     * @param context：上下文
     * @param autoNavi：导航
     */
    public static void navigation(@NonNull Context context, AutoNavi autoNavi) {
        if (autoNavi == null) {
            ToastUtils.showLong("传参AutoNavi为空,不符合规范");
            return;
        }
        try {
            Map<String, String> uriMap = autoNavi.getAutoNavi();
            if (uriMap.isEmpty()) {
                ToastUtils.showLong("传参uriMap为空,不符合规范");
                return;
            }

            //请求URL参数的拼接
            final StringBuilder uriStr = new StringBuilder("amapuri://route/plan?dev=0&t=0");
            for (Map.Entry<String, String> entry : uriMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                uriStr.append("&").append(key).append("=").append(value);
            }

            LogUtils.i("URL参数的拼接:" + uriStr);

            final Uri uri = Uri.parse(uriStr.toString());
            final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addCategory("android.intent.category.DEFAULT");
            ContextCompat.startActivity(context, intent, null);
        } catch (Exception e) {
            ToastUtils.showShort("请安装高德地图");
        }
    }
}
